using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using OrderApi.Models;
using Microsoft.EntityFrameworkCore;

namespace BlogApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class OrderController : ControllerBase
    {
        private readonly OrderContext todoDb;

        public OrderController(OrderContext context)
        {
            this.todoDb = context;
        }

        [HttpGet("{id}")]
        public ActionResult<Order> GetTodoItem(int id)
        {
            var order = todoDb.orders.FirstOrDefault(t => t.OrderId == id);
            if (order == null)
            {
                return NotFound();
            }
            return order;
        }

        [HttpGet]
        public ActionResult<List<Order>> GetOrders(string OrderName, string Customer, float single_price, int amount)
        {
            var query = buildQuery(OrderName, Customer, single_price,amount);
            return query.ToList();
        }

        [HttpGet("pageQuery")]
        public ActionResult<List<TodoItem>> queryOrder(string OrderName, string Customer, float single_price, int amount, int skip, int take)
        {
            var query = buildQuery(OrderName, Customer, single_price, amount).Skip(skip).Take(take);
            return query.ToList();
        }

        private IQueryable<TodoItem> buildQuery(string OrderName, string Customer, float single_price, int amount)
        {
            IQueryable<Order> query = orderDb.orders;
            if (OrderName != null)
            {
                query = query.Where(t => t.OrderName.Contains(OrderName));
            }
            if (Customer != null)
            {
                query = query.Where(t => t.Customer.Contains(Customer));
            }
            if (single_price != null)
            {
                query = query.Where(t => t.single_price.Contains(single_price));
            }
            if (amount != null)
            {
                query = query.Where(t => t.amount.Contains(amount));
            }

            return query;
        }

        [HttpPost]
        public ActionResult<Order> PostOrder(Order o)
        {
            try
            {
                orderDb.orders.Add(o);
                orderDb.SaveChanges();
            }
            catch (Exception e)
            {
                String error= (e.InnerException == null)?e.Message
                    :e.InnerException.Message;
                return BadRequest(error);
            }
            return o;
        }

        [HttpPut("{id}")]
        public ActionResult<Order> PutOrder(int OrderId, Order o)
        {
            if (OrderID != o.OrderId)
            {
                return BadRequest("Id cannot be modified!");
            }
            try
            {
                orderDb.Entry(o).State = EntityState.Modified;
                orderDb.SaveChanges();
            }
            catch (Exception e)
            {
                String error = (e.InnerException == null) ? e.Message
                    : e.InnerException.Message;
                return BadRequest(error);
            }
            return NoContent();
        }

        [HttpDelete("{id}")]
        public ActionResult DeleteOrder(int id)
        {
            try
            {
                var order = orderDb.orders.FirstOrDefault(t => t.OrderId == id);
                if (order != null)
                {
                    orderDb.Remove(order);
                    orderDb.SaveChanges();
                }
            }
            catch (Exception e)
            {
                String error = (e.InnerException == null) ? e.Message
                   : e.InnerException.Message;
                return BadRequest(error);
            }
            return NoContent();
        }
    }
}